<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 根据user-agent获取浏览器版本，操作系统
//-------------------------

class Txcos
{
    /**
     * 腾讯云Cos -- 上传对象
     */
    function qcloudCosUpload($path , $saveName)
    {
        $bucket = 'xslm-1252482422';
        $region = 'cn-sorth';
        $appId = '1252482422';
        $SecretId = 'AKIDSpysYyE3kRta2Fsk4VilfJgEcmhsZJbb';
        $SecretKey = 'uTlbAUZdRyVnnZrVx8uOgaGfw4nth17y';

        //引用COS sdk
        \think\Loader::import('txcos.vendor.autoload');
//        $cosClient = new \Qcloud\Cos\Client(
//            array(
//                'region'      => $region,
//                'credentials' => array(
//                    'appId'     => $appId,
//                    'secretId'  => $SecretId,
//                    'secretKey' => $SecretKey
//                )
//            )
//        );
//
//        $signedUrl = $cosClient->getPresignetUrl('putObject', array(
//            'Bucket' =>$bucket, //存储桶，格式：BucketName-APPID
//            'Key' => $saveName, //对象在存储桶中的位置，即对象键
//            'Body' => '' //可为空或任意字符串
//        ), '+10 minutes'); //签名的有效时间
//
//        $data = array( 'Bucket' => $bucket, 'Key'  => $saveName, 'Body' => fopen($path, 'rb') );
//        dump($data);
//        //判断文件大小 大于5M就分块上传
//        $result = $cosClient->Upload( $data['Bucket'] , $data['Key'] , $data['Body']);
//        dump($result);
//        exit;


        $cosClient = new Qcloud\Cos\Client(
            array(
                'region' => $region,
                'credentials'=> array(
                    'secretId'  => $SecretId ,
                    'secretKey' => $SecretKey)));
            $result = $cosClient->upload(
                $bucket = $bucket, //格式：BucketName-APPID
                $key = 'xslm',
                $body = fopen($path, 'rb'));
            dump($result);

    }
}
